package myTeam.timeShop.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;


public class FileUpLoadUtil {
	private String[] type;
	private Properties properties;
	private static final String propertiesPath = "fileUpload.properties";
	private String uploadPricturePath;
	private long size;
	private boolean isMusic=false;
	private String userFacePath="";
	private String demandPricturePath="";
	private String changePricturePath="";
	private String changePrictureWidth="";
	private String changePrictureHeight="";
	


	public boolean isMusic() {
		return isMusic;
	}

	public void setMusic(boolean isMusic) {
		this.isMusic = isMusic;
	}

	public String getUserFacePath() {
		return userFacePath;
	}

	public void setUserFacePath(String userFacePath) {
		this.userFacePath = userFacePath;
	}

	public String getDemandPricturePath() {
		return demandPricturePath;
	}

	public void setDemandPricturePath(String demandPricturePath) {
		this.demandPricturePath = demandPricturePath;
	}

	public String getChangePricturePath() {
		return changePricturePath;
	}

	public void setChangePricturePath(String changePricturePath) {
		this.changePricturePath = changePricturePath;
	}

	public FileUpLoadUtil() {
		properties = new Properties();
		InputStream in = FileUpLoadUtil.class
				.getResourceAsStream(propertiesPath);
		try {
			properties.load(in);
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		if (properties.containsKey("prictureType")) {
			type = properties.getProperty("prictureType").split("\\.");

		}
		if (properties.containsKey("uploadPricturePath")) {
			uploadPricturePath = properties.getProperty("uploadPricturePath");
		}
		if (properties.containsKey("prictureSize")) {
			size = Long.valueOf(properties.getProperty("prictureSize")) * 1024 * 1024;// 里面的值单位是MB
		}
		if(properties.containsKey("userFacePath")){
			userFacePath=properties.getProperty("userFacePath");
		}
		if(properties.containsKey("demandPricturePath")){
			demandPricturePath=properties.getProperty("demandPricturePath");
		}
		if(properties.containsKey("changePrictureWidth")){
			changePrictureWidth=properties.getProperty("changePrictureWidth");
		}
		if(properties.containsKey("changePrictureHeight")){
			changePrictureHeight=properties.getProperty("changePrictureHeight");
		}
	}

	/**
	 * 
	 * @param item
	 * @return 返回-1,表示类型出现问题,返回-2,表示尺寸过大,文件名表示存入成功,0表示未知错误
	 */
//	public String doUpload(FileItem item, String contentPath) {
//		// TODO 自动生成的方法存根
//		String fileName = item.getName();// 获取完整名字
//		String fileUploadName = fileName
//				.substring(fileName.lastIndexOf("\\") + 1);
//		String typeName = fileUploadName.substring(fileUploadName
//				.lastIndexOf(".") + 1);
//		if (item.getSize() >= size) {
//			return "-2";
//		}
//
//		int count = 0;
//		for (String aType : type) {
//			if (aType.toLowerCase().equals(typeName.toLowerCase())) {
//				count = count + 1;
//			}
//		}
//
//		if (count == 1) {
//			// 往文件名里面加上时间戳
//
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//			int random = (int) (Math.random()*1000000);
//			String timeStamp = sdf.format(new Date());
//			fileUploadName = timeStamp +random+  fileUploadName.substring(fileUploadName
//					.lastIndexOf(".") );
//			try {
//				File file = new File(contentPath + path);
//				if (!file.exists()) {
//					file.mkdirs();
//				}
//
//				item.write(new File(contentPath + path,fileUploadName));
//				//如果是一个图片就进行转化处理
//				if(!this.isMusic){
//					File changeFile = new File(contentPath+changePricturePath);
//					if(!changeFile.exists()){
//						changeFile.mkdirs();
//					}
//					ImageChange ic = new ImageChange(contentPath + path+"\\"+fileUploadName,contentPath + changePricturePath+"\\"+fileUploadName);
//					System.out.print(contentPath);
//					ic.flex(Integer.valueOf(changePrictureWidth),Integer.valueOf(changePrictureHeight));
//					return "/"+changePricturePath+"/"+fileUploadName;
//				}else{
//					return "/"+path+"/"+fileUploadName;
//				}
//				
//
//			} catch (Exception e) {
//				// TODO 自动生成的 catch 块
//				e.printStackTrace();
//	
//				return "0";
//			}
//
//		} else {
//			
//			return "-1";
//		}
//
//	}
	/**
	 * 返回-1,表示类型出现问题,返回-2,表示尺寸过大,文件名表示存入成功,0表示未知错误
	 * jiangjintai
	 * 2016年3月23日
	 * @param image
	 * @param realPath
	 * @return
	 */
	public String doUpload(MultipartFile image, String realPath) {
		// TODO 自动生成的方法存根
		// TODO 自动生成的方法存根
		String fileName = image.getOriginalFilename();// 获取完整名字
		System.out.println("fileName:"+fileName);
		String fileUploadName = fileName
				.substring(fileName.lastIndexOf("\\") + 1);
		System.out.println("fileUploadName:"+fileUploadName);
		String typeName = fileUploadName.substring(fileUploadName
				.lastIndexOf(".") + 1);
		if (image.getSize() >= size) {
			return "-2";
		}

		int count = 0;
		for (String aType : type) {
			if (aType.toLowerCase().equals(typeName.toLowerCase())) {
				count = count + 1;
			}
		}
		System.out.println("count:"+count);
		if (count == 1) {
			// 往文件名里面加上时间戳
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			int random = (int) (Math.random()*1000000);
			String timeStamp = sdf.format(new Date());
			fileUploadName = timeStamp +random+  fileUploadName.substring(fileUploadName
					.lastIndexOf(".") );
			try {
				File file = new File(realPath + uploadPricturePath);
				if (!file.exists()) {
					file.mkdirs();
				}
				//这里用这个方式进行存储
				FileUtils.writeByteArrayToFile(new File(realPath + uploadPricturePath,fileUploadName), image.getBytes());
				//如果是一个图片就进行转化处理
				if(!this.isMusic){
					File changeFile = new File(realPath+changePricturePath);
					if(!changeFile.exists()){
						changeFile.mkdirs();
					}
					File uploadPrictureFile = new File(realPath + uploadPricturePath,fileUploadName);
					File changePrictureFile = new File(realPath + changePricturePath,fileUploadName);
					ImageChange ic = new ImageChange(uploadPrictureFile,changePrictureFile);
					ic.flex(Integer.valueOf(changePrictureWidth),Integer.valueOf(changePrictureHeight));
					System.out.print(realPath);
					return "/"+changePricturePath+"/"+fileUploadName;
				}else{
					return "/"+uploadPricturePath+"/"+fileUploadName;
				}
				

			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
	
				return "0";
			}

		} else {
			
			return "-1";
		}

	}
}
